require.config({ paths: { 'vs': 'vs' } });
// API
var GetText;
var SetText;
var SetTheme;
var SetScroll;
var ShowErr;
var Refresh;

// Enablers
var SwitchMinimap;
var SwitchReadonly;
var SwitchRenderWhitespace;
var SwitchLinks;
var SwitchLineHeight;
var SwitchFontSize;
var SwitchFolding;
var SwitchAutoIndent;
var SwitchFontFamily;
var SwitchFontLigatures;
var AddIntellisense;

// Variables
var editor;
var Proposals = [];

require(['vs/editor/editor.main'], function () {
	function getDependencyProposals() {
		return Proposals;
	}
	
	monaco.languages.registerCompletionItemProvider('python', {
		provideCompletionItems: function(model, position) {
			return getDependencyProposals();
		}
	});
	
	monaco.editor.defineTheme('net-theme-light', {
		base: 'vs',
		inherit: true,
		rules: [
			{ token: 'global', foreground: '000000' },
			{ token: 'keyword', foreground: 'ff6a00' },
			{ token: 'comment', foreground: '666666' },
			{ token: 'number', foreground: 'ffc600' },
			{ token: 'string', foreground: 'ff8c3a' },
		]
	});
	
	monaco.editor.defineTheme('net-theme-dark', {
		base: 'vs-dark',
		inherit: true,
		rules: [
			{ token: 'global', foreground: 'FFFFFF', fontStyle: "bold" },
			{ token: 'keyword', foreground: 'ff6a00', fontStyle: "bold" },
			{ token: 'comment', foreground: '666666' },
			{ token: 'number', foreground: 'ffc600' },
			{ token: 'string', foreground: 'ff8c3a' },
		]
	});
	

	editor = monaco.editor.create(document.getElementById('container'), {
		value: [
			"from btcore import *",
			"from btstruct import *"
		].join('\n'),
		language: 'python',
		theme: "net-theme-light",
		folding: true,
		scrollbar: {
			verticalHasArrows: true,
		},
		dragAndDrop: true,
		links: false,
		minimap: {
			enabled: false,
		},
		showFoldingControls: "always",
		smoothScrolling: true,
	});

	window.onresize = function() {
		editor.layout();
	};
	$(document).bind('keydown', 'Ctrl+s',function (evt){ 
		var text= editor.getValue();
		alert(text);
		return false; });

	/////////////////////////
	GetText = function() {
		return editor.getValue();
	}	  

	SetText = function(x) {
		editor.setValue(x);
	}
	//////////////////////////////
	SetTheme = function(themeName) {
		if (themeName == "Dark") {
			monaco.editor.setTheme("net-theme-dark");
		}
		if (themeName == "Light") {
			monaco.editor.setTheme("net-theme-light");
		}
	}
	
	SwitchMinimap = function(flag) {
		editor.updateOptions({
			minimap: {
				enabled: flag,
			}
		});
	}
	
	SwitchReadonly = function(flag) {
		editor.updateOptions({
			readOnly: flag,
		});
	}
	
	SwitchRenderWhitespace = function(op) {
		editor.updateOptions({
			renderWhitespace: op,
		});
	}
	
	SwitchLinks = function(flag) {
		editor.updateOptions({
			links: flag,
		});
	}
	
	SwitchLineHeight = function(num) {
		editor.updateOptions({
			lineHeight: num,
		});
	}
	
	SwitchFontSize = function(num) {
		editor.updateOptions({
			fontSize: num,
		});
	}
	
	SwitchFolding = function(flag) {
		editor.updateOptions({
			folding: flag,
		});
	}
	
	SwitchAutoIndent = function(flag) {
		editor.updateOptions({
			autoIndent: flag,
		});
	}
	
	SwitchFontFamily = function(name) {
		editor.updateOptions({
			fontFamily: name,
		});
	}
	
	SwitchFontLigatures = function(flag) {
		editor.updateOptions({
			fontLigatures: flag,
		});
	}
	
	
	ShowErr = function(line, column, endline, endcolumn, errMessage) {
		editor.revealPositionInCenter({ lineNumber: line, column: column});
		editor.deltaDecorations([], [
			{ 
				range: new monaco.Range(line, column, endline, endcolumn), 
				options: { 
					inlineClassName: 'squiggly-error',
					hoverMessage: {
						value: errMessage,
					}
				},
			},
		]);
	}		
	
			
	SetScroll = function(line) {
		editor.revealLineInCenter({ lineNumber: line});
	}
	
	Refresh = function() {
		var text = getText();
		setText("");
		editor.trigger('keyboard', 'type', {text: text});
	}
});
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {       
	if(e.target.id=='tabEditor'){
		$("#container").height($("#west").height()-2)
	   editor.layout();
	  }
})
$(document).ready(function () {
	  createLayout(editor);
		   _layout.allowOverflow('north');
});
